package com.cskaoyan.controller;
import com.cskaoyan.bean.CancelOrderBo;
import com.cskaoyan.bean.OrderSubmitBo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.order.*;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.WXOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @title: WXOrderController
 * @author: wangguangwu
 * @Description: 微信订单的控制类
 * @Date: 2021/4/12 7:39 下午
 */
@RestController
@RequestMapping("wx/order/")
public class WXOrderController {

    @Autowired
    UserService userService;
    @Autowired
    WXOrderService orderService;

    @GetMapping("list")
    public BaseRespVo wxOrderList(int showType,int page,int size){
        WxOrderListVo wxOrderListVo=orderService.wxOrderList(showType,page,size);
        return BaseRespVo.success(wxOrderListVo);
    }


    @GetMapping("detail")
    public BaseRespVo wxOrderDetail(Integer orderId){
        OrderWxDetailRespVo orderWxDetailRespVo=null;
        try {
            orderWxDetailRespVo = orderService.wxOrderDetail(orderId);
        }catch (Exception e){
            e.printStackTrace();
            BaseRespVo<Object> objectBaseRespVo = new BaseRespVo<>();
            objectBaseRespVo.setErrmsg("订单详情查询失败");
        }
        return BaseRespVo.success(orderWxDetailRespVo);
    }


    @PostMapping("confirm")
    public BaseRespVo wxOrderConfirm(@RequestBody Map<String,Integer> map){

        Integer orderId = map.get("orderId");
        try {
            orderService.wxOrderConfirm(orderId);
        }catch (Exception e){
            e.printStackTrace();
            BaseRespVo<Object> objectResult = new BaseRespVo<>();
            objectResult.setErrmsg("确认失败");
        }
        return BaseRespVo.success();
    }


    @RequestMapping("goods")
    public  BaseRespVo wxOrderGoods(Integer orderId, Integer goodsId) {
        WxOrderPreCommentVo orderGoods = orderService.wxOrderGoods(orderId,goodsId);
        return  BaseRespVo.success(orderGoods);
    }


    @RequestMapping("prepay")
    public BaseRespVo orderPrepay(){
        return BaseRespVo.success();
    }

    @RequestMapping("comment")
    public BaseRespVo wxOrderComment(@RequestBody OrderWxCommentRequBo orderWxCommentRequBo) {

        WxOrderCommentVo goodsComment = orderService.createGoodsComment(orderWxCommentRequBo);
        return BaseRespVo.success(goodsComment);
    }

    @PostMapping("cancel")
    public BaseRespVo cancelOrder(@RequestBody CancelOrderBo bo){
        Integer orderId = bo.getOrderId();
        orderService.cancelOrderById(orderId);
        BaseRespVo result = new BaseRespVo();
        result.setErrno(102);
        result.setErrmsg("取消订单");
        return result;
    }


    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map<String,Integer> map) {
        orderService.refundOrder(map.get("orderId"));
        return BaseRespVo.success();
    }

    @PostMapping("delete")
    public Object deleteOrder(@RequestBody CancelOrderBo bo){
        Integer orderId = bo.getOrderId();
        orderService.deleteOrderById(orderId);
        return BaseRespVo.success();
    }


    @PostMapping("submit")
    public Object submit(@RequestBody OrderSubmitBo bo){
        Map<String, Object> map = orderService.wxOrderSubmit(bo);

        return BaseRespVo.success();

    }
}
